#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
【需求背景】
在产品端需要上线联系方式来源的快照，现需要对库内的数据情况进行盘点

【需求详情】
库内有接近3.2亿的联系方式，按来源展开，应该会有接近20亿的来源，我需要知道有快照的比例是多少
'''

import sys

from apscheduler.schedulers.blocking import BlockingScheduler

from base import ExportAndTjBase
from models import MongoQueryConfig
from utils import Utils

reload(sys)
sys.setdefaultencoding('utf-8')


class ExportAndTj(ExportAndTjBase):
    def __init__(self, *args, **kwargs):
        super(ExportAndTj, self).__init__(*args, **kwargs)
        # todo other col
        # self.contact_col = self.db.p_col('contact')

    def handle_one_mongo_data(self, record_dic, save_result_dic=None):
        # record_dic = Utils.decrypt_one_record(record_dic, need_decrypt=True, need_strf_time=True)
        # ["contact", "nameId", "name", "socialCreditCode", "regNumber", "contactType", "exportTime"]
        contactChannelList = record_dic.get('contactChannelList')

        must_exists_values = [contactChannelList]  # 这些字段都必须是非空值
        must_has_one_exists_values = []  # 这些字段必须最少有一个是非空值
        save_result_dic['table_contact_count'] += 1
        if self.is_values_has_none(must_exists_values):
            self.write_error_f.write('not all exists: {}'.format(must_exists_values))
        elif self.is_values_all_none(must_has_one_exists_values):
            self.write_error_f.write('not any exists {}'.format(must_has_one_exists_values))
        else:
            for dic in contactChannelList:
                save_result_dic['table_contact_source_count'] += 1
                contactPageImage = dic.get('contactPageImage')
                if contactPageImage:
                    save_result_dic['table_contact_source_has_kz_count'] += 1

        return save_result_dic

    def run(self):
        save_result_dic = {
            'table_contact_count': 0,  # 库内联系方式条数
            'table_contact_source_count': 0,  # 库内联系方式按源展开条数
            'table_contact_source_has_kz_count': 0,  # 库内联系方式按源展开,有快照条数

        }

        config = {
            MongoQueryConfig.col_name: 'contact',
            MongoQueryConfig.filter: {
                "contactChannelList": {"$exists": True}

            },
            MongoQueryConfig.projection: ["contactChannelList"]
        }
        result_dic = self.get_data_from_mongo(config, save_result_dic)


def run(abs_file_name):
    export_and_tj = ExportAndTj(abs_file_name)
    export_and_tj.main()


def scheduler_run(abs_file_name):
    scheduler = BlockingScheduler()
    scheduler.add_job(func=run, trigger='cron',
                      args=(abs_file_name,),
                      # day_of_week="sun",
                      hour='20', minute='52', id='dd_run')
    scheduler.start()


if __name__ == '__main__':
    file_name = 'contact快照摸底'
    # py文件名+file_name+小时时间
    abs_file_name = Utils.generate_abs_path(file_name, current_py_path=__file__)

    run(abs_file_name)
    # scheduler_run(abs_file_name)
